﻿namespace Bowlus.Common.ServiceManagement
{
    using System.Collections.Generic;
    using Bowlus.Common.ServiceManagement.Contracts;
    using System.Linq;

    public class SimulatedServiceManagementWrapper : IServiceManagementWrapper
    {
        public ServiceDeployment GetDeploymentBySlot(string serviceName, string deploymentSlot)
        {
            // deployment(89).Bowlus.Bowlus.AppHost_IN_0
            var instId = Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.CurrentRoleInstance.Id;
            var deplId = instId.Substring(0, instId.IndexOf('.'));

            var deployment = new ServiceDeployment
                {
                    Configuration = "deploymentConfiguration",
                    DeploymentSlot = deplId,
                    ExtensionData = null,
                    Label = deplId,
                    Name = deplId,
                    PrivateId = deplId
                };

            var applicationHostRole = Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.Roles[Settings.AppHostRoleName];
            var appHostInstances = from instance in applicationHostRole.Instances
                                   select
                                       new RoleInstance
                                       {
                                           RoleName = "Bowlus.AppHost",
                                           InstanceName = instance.Id,
                                           InstanceStatus = "Ready"
                                       };

            var frontEndRole = Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.Roles[Settings.FrontEndRoleName];
            var frontInstances = from instance in frontEndRole.Instances
                                   select
                                       new RoleInstance
                                       {
                                           RoleName = "Bowlus.FrontEnd",
                                           InstanceName = instance.Id,
                                           InstanceStatus = "Ready"
                                       };

            var list = new List<RoleInstance>();
            list.AddRange(appHostInstances);
            list.AddRange(frontInstances);

            //var instance = new RoleInstance
            //    {
            //        RoleName = "Bowlus.AppHost",
            //        InstanceName = deplId + ".Bowlus.Bowlus.AppHost_IN_0",
            //        InstanceStatus = "Ready"
            //    };
            //list.Add(instance);

            //instance = new RoleInstance
            //    {
            //        RoleName = "Bowlus.FrontEnd",
            //        InstanceName = deplId + ".Bowlus.Bowlus.FrontEnd_IN_0",
            //        InstanceStatus = "Ready"
            //    };
            //list.Add(instance);

            deployment.RoleInstanceList = new RoleInstanceCollection(list);
            deployment.Status = "Ready";
            deployment.UpgradeDomainCount = 2;
            deployment.UpgradeStatus = null;
            deployment.Url = null; // new Uri("cloudsamurai.cloudapp.net");
            
            return deployment;
        }
    }
}